<?php

require_once(dirname(__FILE__).'/Constants.php');
require_once(dirname(__FILE__).'/Config.php');
require_once(dirname(__FILE__).'/DB.php');
require_once(dirname(__FILE__).'/Util.php');

class Check {
    public $id;
    public $member_id;
    public $visitor_id;
    public $date;
    public $time;
    
    public function Check ($check_id = '') {
        if (!$check_id) return;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf('
            SELECT
                check_id,
                member_id,
                visitor_id,
                check_date,
                check_time
            FROM
                checks
            WHERE
                check_id = %d',
            mysql_real_escape_string($check_id)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        if ($row = mysql_fetch_assoc($result)) {
            $this->id         = $row['check_id'];
            $this->member_id  = $row['member_id'];
            $this->visitor_id = $row['visitor_time'];
            $this->date       = $row['check_date'];
            $this->time       = $row['check_time'];
        }
        return;
    }
    
    public function insert () {
        if (!$this || !$this->member_id || !$this->visitor_id) return FALSE;
        if (!isset($this->flag) || !is_numeric($this->flag)) $this->flag = 0;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            REPLACE INTO
                checks
            SET
                member_id  = '%d',
                visitor_id = '%d',
                check_date = NOW(),
                check_time = NOW()",
            mysql_real_escape_string($this->member_id),
            mysql_real_escape_string($this->visitor_id)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        $this->id   = mysql_insert_id();
        $this->date = date('Y-m-d');
        $this->time = date('H:i:s');
        
        return TRUE;
    }
    
    function delete () {
        if (!$this || !$this->id) return FALSE;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            DELETE FROM
                checks
            WHERE
                check_id = '%d'
            LIMIT 1",
            mysql_real_escape_string($this->id)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return FALSE;
        }
        return mysql_affected_rows();
    }
    
    function get_list ($member_id, $limit = 30) {
        if (!is_numeric($member_id)) return FALSE;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                check_id AS id,
                member_id,
                visitor_id,
                check_date AS date,
                check_time AS time
            FROM
                checks
            WHERE
                member_id = %d
            ORDER BY check_id DESC
            LIMIT %d",
            mysql_real_escape_string($member_id),
            mysql_real_escape_string($limit)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        $checks = array();
        while ($row = mysql_fetch_assoc($result))
            $checks[] = $row;
        return array_reverse($checks);
    }

    function get_count ($member_id) {
        if (!is_numeric($member_id)) return FALSE;
        
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                count(check_id) AS count
            FROM
                checks
            WHERE
                member_id = %d",
            mysql_real_escape_string($member_id)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if (!$row = mysql_fetch_assoc($result))
            return;
        $count = $row['count'];
        return $count;
    }
    
    function get_count_total () {
        $dbh = DB::load_dbh();
        $result = mysql_query('
            SELECT
                MAX(check_id) AS count
            FROM
                checks
        ');
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if ($row = mysql_fetch_assoc($result))
            return $row['count'];
        return;
    }
    
    function get_count_all () {
        $dbh = DB::load_dbh();
        $result = mysql_query('
            SELECT
                COUNT(check_id) AS count
            FROM
                checks
        ');
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if ($row = mysql_fetch_assoc($result))
            return $row['count'];
        return;
    }
    
    function get_count_all_uu () {
        $dbh = DB::load_dbh();
        $result = mysql_query('
            SELECT
                COUNT(DISTINCT(member_id)) AS count
            FROM
                checks
        ');
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if ($row = mysql_fetch_assoc($result))
            return $row['count'];
        return;
    }
    
    function get_count_daily ($target_date) {
        if (!is_date($target_date)) return;
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                COUNT(check_id) AS count
            FROM
                checks
            WHERE
                check_date = '%s'",
            mysql_real_escape_string($target_date)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if ($row = mysql_fetch_assoc($result))
            return $row['count'];
        return;
    }
    
    function get_count_daily_uu ($target_date) {
        if (!is_date($target_date)) return;
        $dbh = DB::load_dbh();
        $result = mysql_query(sprintf("
            SELECT
                COUNT(DISTINCT(member_id)) AS count
            FROM
                checks
            WHERE
                check_date = '%s'",
            mysql_real_escape_string($target_date)
        ));
        if (!$result) {
            trigger_error(mysql_error, E_USER_WARNING);
            return;
        }
        if ($row = mysql_fetch_assoc($result))
            return $row['count'];
        return;
    }
}
